//
//  UIButton+InternalLayout.swift
//  Unibet
//
//  Created by HaoSheng on 2020/5/28.
//  Copyright © 2020 HaoSheng. All rights reserved.
//

import Foundation
import UIKit
import RxSwift
import RxCocoa

extension UIButton {
    public enum ButtonEdgeInsetType {
        case normal                 // 左图右文
        case leftTitleRightImage    // 左文右图
        case topImageBottomTitle    // 上图下文
        case topTitleBottomImage    // 上文下图
    }

    public func changeButtonType(into type: ButtonEdgeInsetType) {
        if self.currentTitle == nil || self.currentImage == nil {
            // 没有标题或者是图片，会居中显示，不需要修改。
            return
        }
        
        if #available(iOS 15.0, *) {
            // 使用 UIButtonConfiguration
            
            var config = UIButton.Configuration.plain()
            
            switch type {
            case .leftTitleRightImage:
                config.imagePlacement = .trailing
            case .topImageBottomTitle:
                config.imagePlacement = .top
            case .topTitleBottomImage:
                config.imagePlacement = .bottom
            default: // 默认是左图右文
                config.imagePlacement = .leading
                break
            }
            self.configuration = config
            
        } else {
            // 回退到 titleEdgeInsets/imageEdgeInsets
            
            // 不能直接使用self.titleLabel.bounds.size来获取宽高，此时，bounds还没有值，需要计算
            let titleWidth = getLabelWidth(with: self.currentTitle, font: (self.titleLabel?.font)!, height: 21)
            let titleHeight = getLabelHeight(with: self.currentTitle, font: (self.titleLabel?.font)!, width: titleWidth)
            let imageSize = (self.currentImage?.size)!
            
            switch type {
            case .leftTitleRightImage:
                self.titleEdgeInsets = .init(top: 0, left: -imageSize.width, bottom: 0, right: imageSize.width)
                self.imageEdgeInsets = .init(top: 0, left: titleWidth, bottom: 0, right: -titleWidth)
                break
            case .topImageBottomTitle:
                self.titleEdgeInsets = .init(top: imageSize.height / 2 + 5, left: -imageSize.width / 2, bottom: -imageSize.height / 2 - 5, right: imageSize.width / 2)
                self.imageEdgeInsets = .init(top: -titleHeight / 2 - 5, left: titleWidth / 2, bottom: titleHeight / 2 + 5, right: -titleWidth / 2)
                break
            case .topTitleBottomImage:
                self.imageEdgeInsets = .init(top: titleHeight / 2, left: titleWidth / 2, bottom: -titleHeight / 2, right: -titleWidth / 2)
                self.titleEdgeInsets = .init(top: -imageSize.height / 2, left: 0, bottom: imageSize.height / 2, right: imageSize.width)
                break
            default:
                // 默认是左图右文，也不需要做任何修改。
                self.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0)
                self.titleEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 0)
                break
            }
        }
        
    }
    
    
    public func getLabelWidth(with content: String?, font: UIFont, height: CGFloat) -> CGFloat {
        if content == nil || (content?.count)! <= 0 {
            return 0
        }
        
        let size = CGSize.init(width: CGFloat(MAXFLOAT), height: height)
        let nsContent = content! as NSString
        let returnSize = nsContent.boundingRect(with: size, options: [.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font: font], context: nil)
        
        return returnSize.width
    }

    public func getLabelHeight(with content: String?, font: UIFont, width: CGFloat) -> CGFloat {
        if content == nil || (content?.count)! <= 0 {
            return 0
        }
        
        let size = CGSize.init(width: width, height: CGFloat(MAXFLOAT))
        let nsContent = content! as NSString
        let returnSize = nsContent.boundingRect(with: size, options: [.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font: font], context: nil)
        
        return returnSize.height
    }
    
    public func setContentInsets(insets:UIEdgeInsets){
        if #available(iOS 15.0, *) {
            var config = self.configuration ?? UIButton.Configuration.plain()
            config.contentInsets = NSDirectionalEdgeInsets(top: insets.top, leading: insets.left, bottom: insets.bottom, trailing: insets.right)
            config.background.backgroundColor = .clear
            configuration = config
        } else {
            contentEdgeInsets = insets
        }
    }
    
    
    public func setFont(font:CGFloat){
        if #available(iOS 15.0, *) {
            var config = self.configuration ?? UIButton.Configuration.plain()
            config.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
                var outgoing = incoming
                outgoing.font = UIFont.systemFont(ofSize: font) // 设置字号 12
                return outgoing
            }
            configuration = config
        } else {
            titleLabel?.font = UIFont.systemFont(ofSize: font)
        }
    }
    
}


@MainActor
extension Reactive where Base: UIButton {
    public var isSelected: ControlProperty<Bool> {
        return base.rx.controlProperty(
            editingEvents: [.touchUpInside],
            getter: { button in button.isSelected },
            setter: { button, value in button.isSelected = value }
        )
    }
}
